package com.ericsson.common.util;

import java.io.*;
import java.util.zip.GZIPInputStream;

public class UncompressFileGZIPUtil {
    /**
     * Uncompress the incoming file.
     *
     * @param inFileName
     *            Name of the file to be uncompressed
     */
    public static void doProcUncompressFile(String path, File f, String inFileName) {

        try {
            GZIPInputStream in = null;
            try {
                in = new GZIPInputStream(new FileInputStream(f));
            } catch (FileNotFoundException e) {
            }
            String outFileName = path.substring(0,path.lastIndexOf("/")+1) + getFileName(inFileName);
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(outFileName);
            } catch (FileNotFoundException e) {
            }
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();

        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }

    }


    /**
     * Uncompress the incoming file.
     *
     * @param inFileName
     *            Name of the file to be uncompressed
     */
    public static void doUncompressFile(String path, File f, String inFileName) {

        try {
            GZIPInputStream in = null;
            try {
                in = new GZIPInputStream(new FileInputStream(f));
            } catch (FileNotFoundException e) {
            }
            String outFileName = path + getFileName(inFileName);
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(outFileName);
            } catch (FileNotFoundException e) {
            }
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();

        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }

    }

    /**
     * Used to extract and return the extension of a given file.
     *
     * @param f
     *            Incoming file to get the extension of
     * @return <code>String</code> representing the extension of the incoming
     *         file.
     */
    public static String getExtension(String f) {
        String ext = "";
        int i = f.lastIndexOf('.');

        if (i > 0 && i < f.length() - 1) {
            ext = f.substring(i + 1);
        }
        return ext;
    }

    /**
     * Used to extract the filename without its extension.
     *
     * @param f
     *            Incoming file to get the filename
     * @return <code>String</code> representing the filename without its
     *         extension.
     */
    public static String getFileName(String f) {
        String fname = "";
        int i = f.lastIndexOf('.');

        if (i > 0 && i < f.length() - 1) {
            fname = f.substring(0, i);
        }
        return fname;
    }
}
